home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / sun / jpeg / lib / jddeflts.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-15  |  7.1 KB  |  186 lines

  1. /*
  2.  * jddeflts.c
  3.  *
  4.  * Copyright (C) 1991, 1992, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains optional default-setting code for the JPEG decompressor.
  9.  * User interfaces do not have to use this file, but those that don't use it
  10.  * must know more about the innards of the JPEG code.
  11. %
  12. % Modified:     Jin Guojun - LBL, Image Technology Group
  13. %       Date:   April 14, 1992
  14. %       Goal:   To be easily handled by conversion library - CCS (c)
  15. %        HIPS, FITS, GIF, RLE, SUN-raster, PNM, TIFF, PICT ...
  16. %        can be compressed by cjpeg now, directly displayed by tuner,
  17. %        decompressed to other image type by torle, torast, and color_ps.
  18. %        These type of images can be determined by program `headers'.
  19.  */
  20.  
  21. #include "jinclude.h"
  22.  
  23.  
  24. /* Default do-nothing progress monitoring routine.
  25.  * This can be overridden by a user interface that wishes to
  26.  * provide progress monitoring; just set methods->progress_monitor
  27.  * after j_d_defaults is done.  The routine will be called periodically
  28.  * during the decompression process.
  29.  *
  30.  * During any one pass, loopcounter increases from 0 up to (not including)
  31.  * looplimit; the step size is not necessarily 1.  Both the step size and
  32.  * the limit may differ between passes.  The expected total number of passes
  33.  * is in cinfo->total_passes, and the number of passes already completed is
  34.  * in cinfo->completed_passes.  Thus the fraction of work completed may be
  35.  * estimated as
  36.  *        completed_passes + (loopcounter/looplimit)
  37.  *        ------------------------------------------
  38.  *                total_passes
  39.  * ignoring the fact that the passes may not be equal amounts of work.
  40.  *
  41.  * When decompressing, the total_passes figure is an estimate that may be
  42.  * on the high side; completed_passes will jump by more than one if some
  43.  * passes are skipped.
  44.  */
  45.  
  46. METHODDEF void
  47. progress_monitor (decompress_info_ptr cinfo, long loopcounter, long looplimit)
  48. {
  49.   /* do nothing */
  50. }
  51.  
  52.  
  53. /*
  54.  * Reload the input buffer after it's been emptied, and return the next byte.
  55.  * See the JGETC macro for calling conditions.  Note in particular that
  56.  * read_jpeg_data may NOT return EOF.  If no more data is available, it must
  57.  * exit via ERREXIT, or perhaps synthesize fake data (such as an RST marker).
  58.  * In the present implementation, we insert an EOI marker; this might not be
  59.  * appropriate for non-JFIF file formats, but it usually allows us to handle
  60.  * a truncated JFIF file.
  61.  *
  62.  * This routine can be overridden by the system-dependent user interface,
  63.  * in case the data source is not a stdio stream or some other special
  64.  * condition applies.  Note, however, that this capability only applies for
  65.  * JFIF or similar serial-access JPEG file formats.  The input file control
  66.  * module for a random-access format such as TIFF/JPEG would most likely
  67.  * override the read_jpeg_data method with its own routine.
  68.  */
  69.  
  70. static    int    jpeg_read_size =
  71. #ifdef    STREAM_IMAGE
  72.         1;    /* be set to JPEG_BUF_SIZE by JPEG */
  73. #else
  74.         JPEG_BUF_SIZE;
  75. #endif
  76.  
  77. METHODDEF int
  78. read_jpeg_data (decompress_info_ptr cinfo)
  79. {
  80.   cinfo->next_input_byte = cinfo->input_buffer + MIN_UNGET;
  81. #ifndef    STREAM_IMAGE
  82.   cinfo->bytes_in_buffer = (int) JFREAD(cinfo->input_file,
  83.                     cinfo->next_input_byte,
  84.                     jpeg_read_size);
  85. #else
  86.   cinfo->bytes_in_buffer = (int) JFREAD(cinfo->input_file,
  87.                     cinfo->next_input_byte,
  88.     cinfo->img->tmp_offset ? cinfo->img->tmp_offset : jpeg_read_size);
  89.     cinfo->img->tmp_offset = 1;
  90. #endif
  91.  
  92.   if (cinfo->bytes_in_buffer <= 0) {
  93.     WARNMS(cinfo->emethods, "Premature EOF in JPEG file");
  94.     cinfo->next_input_byte[0] = (char) 0xFF;
  95.     cinfo->next_input_byte[1] = (char) 0xD9; /* EOI marker */
  96.     cinfo->bytes_in_buffer = 2;
  97. /*    return    prgmerr(0, "Premature EOF in JPEG file");    */
  98.   }
  99.  
  100.   return JGETC(cinfo);
  101. }
  102.  
  103.  
  104.  
  105. /* Default parameter setup for decompression.
  106.  *
  107.  * User interfaces that don't choose to use this routine must do their
  108.  * own setup of all these parameters.  Alternately, you can call this
  109.  * to establish defaults and then alter parameters selectively.  This
  110.  * is the recommended approach since, if we add any new parameters,
  111.  * your code will still work (they'll be set to reasonable defaults).
  112.  *
  113.  * standard_buffering should be TRUE to cause an input buffer to be allocated
  114.  * (the normal case); if FALSE, the user interface must provide a buffer.
  115.  * This option is most useful in the case that the buffer must not be freed
  116.  * at the end of an image.  (For example, when reading a sequence of images
  117.  * from a single file, the remaining data in the buffer represents the
  118.  * start of the next image and mustn't be discarded.)  To handle this,
  119.  * allocate the input buffer yourself at startup, WITHOUT using alloc_small
  120.  * (probably a direct call to malloc() instead).  Then pass FALSE on each
  121.  * call to j_d_defaults to ensure the buffer state is not modified.
  122.  *
  123.  * If the source of the JPEG data is not a stdio stream, override the
  124.  * read_jpeg_data method with your own routine after calling j_d_defaults.
  125.  * You can still use the standard buffer if it's appropriate.
  126.  *
  127.  * CAUTION: if you want to decompress multiple images per run, it's necessary
  128.  * to call j_d_defaults before *each* call to jpeg_decompress, since subsidiary
  129.  * structures like the quantization tables are automatically freed during
  130.  * cleanup.
  131.  */
  132.  
  133. GLOBAL void
  134. j_d_defaults (decompress_info_ptr cinfo, boolean standard_buffering)
  135. /* NB: the external methods must already be set up. */
  136. {
  137.   short i;
  138.  
  139.   /* Initialize pointers as needed to mark stuff unallocated. */
  140.   /* Outer application may fill in default tables for abbreviated files... */
  141.   cinfo->comp_info = NULL;
  142.   for (i = 0; i < NUM_QUANT_TBLS; i++)
  143.     cinfo->quant_tbl_ptrs[i] = NULL;
  144.   for (i = 0; i < NUM_HUFF_TBLS; i++) {
  145.     cinfo->dc_huff_tbl_ptrs[i] = NULL;
  146.     cinfo->ac_huff_tbl_ptrs[i] = NULL;
  147.   }
  148.   cinfo->colormap = NULL;
  149.  
  150.   /* Default to RGB output */
  151.   /* UI can override by changing out_color_space */
  152.   cinfo->out_color_space = CS_RGB;
  153.   cinfo->jpeg_color_space = CS_UNKNOWN;
  154.   /* Setting any other value in jpeg_color_space overrides heuristics in */
  155.   /* jrdjfif.c.  That might be useful when reading non-JFIF JPEG files, */
  156.   /* but ordinarily the UI shouldn't change it. */
  157.   
  158.   /* Default to no gamma correction of output */
  159.   cinfo->output_gamma = 1.0;
  160.   
  161.   /* Default to no color quantization */
  162.   cinfo->quantize_colors = FALSE;
  163.   /* but set reasonable default parameters for quantization, */
  164.   /* so that turning on quantize_colors is sufficient to do something useful */
  165.   cinfo->two_pass_quantize = TRUE;
  166.   cinfo->use_dithering = TRUE;
  167.   cinfo->desired_number_of_colors = 256;
  168.   
  169.   /* Default to no smoothing */
  170.   cinfo->do_block_smoothing = FALSE;
  171.   cinfo->do_pixel_smoothing = FALSE;
  172.   
  173.   /* Allocate memory for input buffer, unless outer application provides it. */
  174.   if (standard_buffering) {
  175.     verify_buffer_size(&cinfo->input_buffer, 1,
  176.                 JPEG_BUF_SIZE + MIN_UNGET, "jibuf");
  177.     cinfo->bytes_in_buffer = 0;    /* initialize buffer to empty */
  178.   }
  179.  
  180.   /* Install standard buffer-reloading method (outer code may override). */
  181.   cinfo->methods->read_jpeg_data = read_jpeg_data;
  182.  
  183.   /* Install default do-nothing progress monitoring method. */
  184.   cinfo->methods->progress_monitor = progress_monitor;
  185. }
  186.